package l2s.gameserver.scripts.custom;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import l2s.commons.configuration.Config;
import l2s.commons.db.DatabaseFactory;
import l2s.gameserver.datatables.CharNameTable;
import l2s.gameserver.datatables.ClanTable;
import l2s.gameserver.datatables.SkillTable;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.olympiad.OlympiadManager;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.network.serverpackets.MagicSkillUse;
import l2s.gameserver.scripting.ScriptFile;

/**
 * @author TrueMan
 * @project Silentium
 * @version 1.0
 */
public class Services extends Quest implements ScriptFile
{
	private static final Logger _log = LoggerFactory.getLogger(Services.class.getName());
	
	// ID НПЦ
	private static final int ServicesNpcId = Config.SERVICE_NPC_ID;
	
	// Продажа нубла
	private static final int nobleItemId = Config.NOBLE_ITEM_ID;
	private static final int nobleItemCount = Config.NOBLE_ITEM_COUNT;
	
	// Продажа фул клан скилов
	private static final int skillItemId = Config.FULL_SKILL_ITEM_ID;
	private static final int skillItemCount = Config.FULL_SKILL_ITEM_COUNT;
	
	// Смена ника
	private static final int changeNameItemId = Config.CHANGE_NAME_ITEM_ID;
	private static final int changeNameItemCount = Config.CHANGE_NAME_ITEM_COUNT;
	
	// Покраска ника и титула
	private static final int changeColorItemId = Config.CHANGE_COLOR_ITEM_ID;
	private static final int changeColorItemCount = Config.CHANGE_COLOR_ITEM_COUNT;
	
	// Смена имени клана
	private static final int changeClanNameItemId = Config.CHANGE_CLAN_NAME_ITEM_ID;
	private static long changeClanNameItemCount = Config.CHANGE_CLAN_NAME_ITEM_COUNT;
	private static final int clanMinLevel = Config.CLAN_MIN_LEVEL;
	
	// Продажа уровня клана
	private static final int clanLevelItemsId6 = Config.CLAN_LEVEL6_ITEM_ID;
	private static final int clanLevelItemsCount6 = Config.CLAN_LEVEL6_ITEM_COUNT;
	private static final int clanLevelItemsId7 = Config.CLAN_LEVEL7_ITEM_ID;
	private static final int clanLevelItemsCount7 = Config.CLAN_LEVEL7_ITEM_COUNT;
	private static final int clanLevelItemsId8 = Config.CLAN_LEVEL8_ITEM_ID;
	private static final int clanLevelItemsCount8 = Config.CLAN_LEVEL8_ITEM_COUNT;
	
	// Репутация клана
	private static final int clanReputationPointsItemId = Config.REPUTATION_ITEM_ID;
	private static final int clanReputationPointsItemCount = Config.REPUTATION_ITEM_COUNT;
	private static final int clanReputationPointsItemId1 = Config.REPUTATION_ITEM_ID1;
	private static final int clanReputationPointsItemCount1 = Config.REPUTATION_ITEM_COUNT1;
	private static final int clanReputationPointsItemId2 = Config.REPUTATION_ITEM_ID2;
	private static final int clanReputationPointsItemCount2 = Config.REPUTATION_ITEM_COUNT2;
	
	// Смена пола
	private static final int changeGenderItemId = Config.CHANGE_GENDER_ITEM_ID;
	private static final int changeGenderItemCount = Config.CHANGE_GENDER_ITEM_COUNT;
	
	// Премиум
	private static final int premiumItemId = Config.PREMIUM_ITEM_ID;
	private static final int premiumItemCount = Config.PREMIUM_ITEM_COUNT;
	
	public Services(int questId, String name, String descr)
	{
		super(questId, name, descr);
		
		addStartNpc(ServicesNpcId);
		addFirstTalkId(ServicesNpcId);
		addTalkId(ServicesNpcId);
	}
	
	public static void onLoad()
	{
		new Services(-1, Services.class.getSimpleName(), "custom");
	}
	
	@Override
	public String onFirstTalk(L2Npc npc, L2PcInstance player)
	{
		if (player.getQuestState(getName()) == null)
		{
			newQuestState(player);
		}
		else if (player.isInCombat() || player.getPvpFlag() == 1 || player.getKarma() != 0 || OlympiadManager.getInstance().isRegistered(player) || player.isDead() || player.isFakeDeath())
		{
			return "Services-Blocked.htm";
		}
		
		return "Services.htm";
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmlText = event;
		QuestState st = player.getQuestState(getName());
		
		if (event.equals("setNoble"))
		{
			if (!player.isNoble())
			{
				if (st.getQuestItemsCount(nobleItemId) >= nobleItemCount)
				{
					st.takeItems(nobleItemId, nobleItemCount);
					player.setNoble(true, true);
					player.setTarget(player);
					player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
					player.broadcastUserInfo();
					return "NoblesseServices-Success.htm";
				}
				return "NoblesseServices-NoItems.htm";
			}
			return "NoblesseServices-AlredyNoble.htm";
		}
		else if (event.startsWith("levelUp"))
		{
			if (player.isClanLeader())
			{
				if (event.equals("levelUp6Clan"))
				{
					if (st.getQuestItemsCount(clanLevelItemsId6) >= clanLevelItemsCount6)
					{
						st.takeItems(clanLevelItemsId6, clanLevelItemsCount6);
						player.getClan().changeLevel(6);
						player.getClan().broadcastClanStatus();
						player.sendMessage("Your clan is now level " + player.getClan().getLevel() + ".");
						player.setTarget(player);
						player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
				else if (event.equals("levelUp7Clan"))
				{
					if (st.getQuestItemsCount(clanLevelItemsId7) >= clanLevelItemsCount7)
					{
						st.takeItems(clanLevelItemsId7, clanLevelItemsCount7);
						player.getClan().changeLevel(7);
						player.getClan().broadcastClanStatus();
						player.sendMessage("Your clan is now level " + player.getClan().getLevel() + ".");
						player.setTarget(player);
						player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
				else if (event.equals("levelUp8Clan"))
				{
					if (st.getQuestItemsCount(clanLevelItemsId8) >= clanLevelItemsCount8)
					{
						st.takeItems(clanLevelItemsId8, clanLevelItemsCount8);
						player.getClan().changeLevel(8);
						player.getClan().broadcastClanStatus();
						player.sendMessage("Your clan is now level " + player.getClan().getLevel() + ".");
						player.setTarget(player);
						player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
			}
			else
			{
				return "ClanLevelUp-NoLeader.htm";
			}
		}
		else if (event.equals("setClanSkill"))
		{
			if (player.isClanLeader() && player.getClan().getLevel() == 8)
			{
				if (st.getQuestItemsCount(skillItemId) >= skillItemCount)
				{
					st.takeItems(skillItemId, skillItemCount);
					player.setTarget(player);
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(370, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(371, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(372, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(373, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(374, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(375, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(376, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(377, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(378, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(379, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(380, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(381, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(382, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(383, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(384, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(385, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(386, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(387, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(388, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(389, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(390, 3));
					st.getPlayer().getClan().addNewSkill(SkillTable.getInstance().getInfo(391, 1));
					player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
					player.broadcastUserInfo();
					return "ClanSkillServices-Success.htm";
				}
				return "ClanSkillServices-NoItems.htm";
			}
			return "ClanSkillServices-Error.htm";
		}
		else if (event.equals("changeGender"))
		{
			if (st.getQuestItemsCount(changeGenderItemId) >= changeGenderItemCount)
			{
				st.takeItems(changeGenderItemId, changeGenderItemCount);
				player.getAppearance().setSex(player.getAppearance().getSex() ? false : true);
				player.setTarget(player);
				player.broadcastPacket(new MagicSkillUse(player, 5103, 1, 1000, 0));
				player.broadcastUserInfo();
				
				return "ChangeGender-Success.htm";
			}
			return "ChangeGender-NoItems.htm";
		}
		else if (event.startsWith("changeName"))
		{
			try
			{
				String newName = event.substring(11);
				
				if (st.getQuestItemsCount(changeNameItemId) >= changeNameItemCount)
				{
					if (newName == null)
					{
						return "ChangeName.htm";
					}
					if (!newName.matches("^[a-zA-Z0-9]+$"))
					{
						player.sendMessage("Incorrect name. Please try again.");
						return "ChangeName.htm";
					}
					else if (newName.equals(player.getName()))
					{
						player.sendMessage("Please, choose a different name.");
						return "ChangeName.htm";
					}
					else if (CharNameTable.doesCharNameExist(newName))
					{
						player.sendMessage("The name " + newName + " already exists.");
						return "ChangeName.htm";
					}
					else
					{
						st.takeItems(changeNameItemId, changeNameItemCount);
						player.setName(newName);
						player.store();
						player.sendMessage("Your new character name is " + newName);
						player.broadcastUserInfo();
						player.getClan().broadcastClanStatus();
						
						return "ChangeName-Success.htm";
					}
				}
				return "ChangeName-NoItems.htm";
			}
			catch (Exception e)
			{
				player.sendMessage("Вы успешно сменили ник!");
				return "ChangeName.htm";
			}
		}
		
		else if (event.equals("Color1"))
		{
			if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
			{
				st.takeItems(changeColorItemId, changeColorItemCount);
				st.getPlayer().getAppearance().setNameColor(0x993399);
				player.broadcastUserInfo();
				return "ColorServices-Success.htm";
			}
			return "ColorServices-NoItems.htm";
		}
		
		else if (event.equals("Color2"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x00CCCC);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color3"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x0099FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color4"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0xCC00CC);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color5"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x3366FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color6"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x0000CC);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color7"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x9999CC);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color8"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0xCC33FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color9"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0xCC99FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color10"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x003366);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color11"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x333366);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color12"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x990099);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color13"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x003399);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color14"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setNameColor(0x000000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color15"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x000000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color16"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x000090);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color17"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x0000ff);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color18"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x009000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color19"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x00FF00);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color20"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x900000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color21"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0xFF0000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color22"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x009090);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color23"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x0090FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color24"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x00FFFF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color25"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x900090);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color26"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x9000FF);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color27"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0xff00ff);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color28"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0x909000);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color29"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0xFFFF00);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		
		else if (event.equals("Color30"))
		{
			{
				if (st.getQuestItemsCount(changeColorItemId) >= changeColorItemCount)
				{
					st.takeItems(changeColorItemId, changeColorItemCount);
					st.getPlayer().getAppearance().setTitleColor(0xffffff);
					player.broadcastUserInfo();
					return "ColorServices-Success.htm";
				}
				return "ColorServices-NoItems.htm";
			}
		}
		else if (event.startsWith("changeClanName"))
		{
			if (player.getClan() == null)
			{
				return "ChangeClanName-NoClan.htm";
			}
			try
			{
				String newClanName = event.substring(15);
				
				if (st.getQuestItemsCount(changeClanNameItemId) >= changeClanNameItemCount)
				{
					if (newClanName == null)
					{
						return "ChangeClanName.htm";
					}
					if (!player.isClanLeader())
					{
						player.sendMessage("Only the clan leader can change the clan name.");
						return "ChangeClanName.htm";
					}
					else if (player.getClan().getLevel() < clanMinLevel)
					{
						player.sendMessage("Your clan must be at least level " + clanMinLevel + " to change the name.");
						return "ChangeClanName.htm";
					}
					else if (!newClanName.matches("^[a-zA-Z0-9]+$"))
					{
						player.sendMessage("Incorrect name. Please try again.");
						return "ChangeClanName.htm";
					}
					else if (newClanName.equals(player.getClan().getName()))
					{
						player.sendMessage("Please, choose a different name.");
						return "ChangeClanName.htm";
					}
					else if (ClanTable.getInstance().getClanByName(newClanName) != null)
					{
						player.sendMessage("The name " + newClanName + " already exists.");
						return "ChangeClanName.htm";
					}
					else
					{
						st.takeItems(changeNameItemId, changeNameItemCount);
						player.getClan().setName(newClanName);
						
						try (Connection con = DatabaseFactory.getConnection())
						{
							PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET clan_name=? WHERE clan_id=?");
							statement.setString(1, newClanName);
							statement.setInt(2, player.getClan().getClanId());
							statement.execute();
							statement.close();
						}
						catch (Exception e)
						{
							_log.info("Error updating clan name for player " + player.getName() + ". Error: " + e);
						}
						
						player.sendMessage("Your new clan name is " + newClanName);
						player.getClan().broadcastClanStatus();
						
						return "ChangeClanName-Success.htm";
					}
				}
				return "ChangeClanName-NoItems.htm";
			}
			catch (Exception e)
			{
				player.sendMessage("Please, insert a correct name.");
				return "ChangeClanName.htm";
			}
		}
		else if (event.startsWith("crp"))
		{
			if (player.isClanLeader())
			{
				if (event.equals("crp1"))
				{
					if (st.getQuestItemsCount(clanReputationPointsItemId) >= clanReputationPointsItemCount)
					{
						st.takeItems(clanReputationPointsItemId, clanReputationPointsItemCount);
						player.getClan().addReputationScore(5000);
						player.getClan().broadcastClanStatus();
						
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
				else if (event.equals("crp2"))
				{
					if (st.getQuestItemsCount(clanReputationPointsItemId1) >= clanReputationPointsItemCount1)
					{
						st.takeItems(clanReputationPointsItemId1, clanReputationPointsItemCount1);
						player.getClan().addReputationScore(10000);
						player.getClan().broadcastClanStatus();
						
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
				else if (event.equals("crp3"))
				{
					if (st.getQuestItemsCount(clanReputationPointsItemId2) >= clanReputationPointsItemCount2)
					{
						st.takeItems(clanReputationPointsItemId1, clanReputationPointsItemCount1);
						player.getClan().addReputationScore(20000);
						player.getClan().broadcastClanStatus();
						
						return "ClanLevelUp.htm";
					}
					return "ClanLevelUp-NoItems.htm";
				}
			}
		}
		else if (event.equals("premium1"))
		{
			if (st.getQuestItemsCount(premiumItemId) >= premiumItemCount)
			{
				st.takeItems(premiumItemId, premiumItemCount);
				addPremiumService(24, player);
				return "PremiumAccount-Success.htm";
			}
			return "PremiumAccount-NoItems.htm";
		}
		else if (event.equals("premium2"))
		{
			if (st.getQuestItemsCount(premiumItemId) >= (premiumItemCount * 2))
			{
				st.takeItems(premiumItemId, premiumItemCount * 2);
				addPremiumService(24 * 3, player);
				return "PremiumAccount-Success.htm";
			}
			return "PremiumAccount-NoItems.htm";
		}
		else if (event.equals("premium3"))
		{
			if (st.getQuestItemsCount(premiumItemId) >= (premiumItemCount * 3))
			{
				st.takeItems(premiumItemId, premiumItemCount * 3);
				addPremiumService(24 * 7, player);
				return "PremiumAccount-Success.htm";
			}
			return "PremiumAccount-NoItems.htm";
		}
		else if (event.equals("premium4"))
		{
			if (st.getQuestItemsCount(premiumItemId) >= (premiumItemCount * 3))
			{
				st.takeItems(premiumItemId, premiumItemCount * 3);
				addPremiumService(24 * 14, player);
				return "PremiumAccount-Success.htm";
			}
			return "PremiumAccount-NoItems.htm";
		}
		else if (event.equals("premium5"))
		{
			if (st.getQuestItemsCount(premiumItemId) >= (premiumItemCount * 3))
			{
				st.takeItems(premiumItemId, premiumItemCount * 3);
				addPremiumService(24 * 30, player);
				return "PremiumAccount-Success.htm";
			}
			return "PremiumAccount-NoItems.htm";
		}
		return htmlText;
	}
	
	private static void addPremiumService(int hours, L2PcInstance player)
	{
		try (Connection con = DatabaseFactory.getConnection())
		{
			Calendar finishtime = Calendar.getInstance();
			finishtime.setTimeInMillis(System.currentTimeMillis());
			finishtime.set(Calendar.SECOND, 0);
			finishtime.add(Calendar.HOUR, hours);
			
			PreparedStatement statement = con.prepareStatement("UPDATE account_premium SET premium_service=?,enddate=? WHERE account_name=?");
			statement.setInt(1, 1);
			statement.setLong(2, finishtime.getTimeInMillis());
			statement.setString(3, player.getAccountName());
			statement.execute();
			statement.close();
		}
		catch (SQLException e)
		{
			_log.info("EventPremiumNpc:  Could not increase data");
		}
	}
}
